#include "MPlot.h"
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QDebug>

MPlot::MPlot(QCustomPlot *parent)
    : customPlot(parent), time(0.0)
{
    // 创建QCustomPlot
    // customPlot = new QCustomPlot(this);
    // QVBoxLayout *layout = new QVBoxLayout(this);
    // layout->addWidget(customPlot);
    // setLayout(layout);

    // 设置图形
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    customPlot->xAxis->setLabel("Time (s)");
    customPlot->yAxis->setLabel("Amplitude");
    customPlot->xAxis->setRange(0, 10);
    customPlot->yAxis->setRange(-1, 1);

    // 启用交互
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    // 定时器，用于更新图形
    dataTimer = new QTimer(this);
    connect(dataTimer, &QTimer::timeout, this, &MPlot::updateGraph);
}

MPlot::~MPlot()
{
}

void MPlot::start()
{
    dataTimer->start(50); // 每50ms更新一次
}

void MPlot::stop()
{
    dataTimer->stop();
}

void MPlot::clearData()
{
    xData.clear();
    yData.clear();
}

void MPlot::updateGraph()
{
    time += 0.05;
    double newValue = qSin(time * 3.1415);  // 示例波形数据

    // 更新数据
    xData.append(time);
    yData.append(newValue);

    // 控制数据长度
    if (xData.size() > 2000) {
        xData.removeFirst();
        yData.removeFirst();
    }

    customPlot->graph(0)->setData(xData, yData);
    customPlot->xAxis->setRange(time - 10, time);  // 显示最近10秒的数据
    customPlot->replot();
}

void MPlot::append(double data)
{
    time += 0.01;
    xData.append(time);
    yData.append(data);

    if (xData.size() > 1000) {
        xData.removeFirst();
        yData.removeFirst();
    }
    customPlot->graph(0)->setData(xData, yData);
    customPlot->xAxis->setRange(time - 10, time);  // 显示最近10秒的数据
    customPlot->replot();
}

void MPlot::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        double x = customPlot->xAxis->pixelToCoord(event->pos().x());
        double y = customPlot->yAxis->pixelToCoord(event->pos().y());
        qDebug() << "Left-click at:" << QPointF(x, y);
    }
    else if (event->button() == Qt::RightButton) {
        rightClickStartPos = event->pos();  // 记录右键点击位置
    }
}

void MPlot::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton) {
        qDebug() << "Right-click released.";
    }
}

void MPlot::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::RightButton) {
        // 拖动实现平移
        double dx = customPlot->xAxis->pixelToCoord(event->pos().x()) - customPlot->xAxis->pixelToCoord(rightClickStartPos.x());
        double dy = customPlot->yAxis->pixelToCoord(event->pos().y()) - customPlot->yAxis->pixelToCoord(rightClickStartPos.y());

        customPlot->xAxis->setRange(customPlot->xAxis->range().lower - dx, customPlot->xAxis->range().upper - dx);
        customPlot->yAxis->setRange(customPlot->yAxis->range().lower - dy, customPlot->yAxis->range().upper - dy);
        customPlot->replot();
    }
}

void MPlot::wheelEvent(QWheelEvent *event)
{
    double factor = (event->angleDelta().y() > 0) ? 1.1 : 0.9;
    customPlot->xAxis->scaleRange(factor, customPlot->xAxis->pixelToCoord(event->position().x()));
    customPlot->yAxis->scaleRange(factor, customPlot->yAxis->pixelToCoord(event->position().y()));
    customPlot->replot();
}
